﻿using EnterpriseInformation.Common;
using EnterpriseInformation.DataModel;
using Newtonsoft.Json.Linq;
using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace EnterpriseInformation
{
    public class ServiceControl:CustomLog
    {
        public ServiceControl() 
        {
            InitLog("全局");
        }


        public void test()
        {
            HttpUtil httpUtil = new HttpUtil();
            httpUtil.RetryVerify();
        }

        public ResultModel HttpGet(string url, string Unique)
        {
            ResultModel resultDetail = new ResultModel();
            try
            {
                HttpUtil httpUtil = new HttpUtil();

                resultDetail = httpUtil.HttpGet(url);

                //Log.Logger.Information("resultDetail.Html:" + resultDetail.Html);

                if (resultDetail.Html.Contains("index_verify"))
                {
                    Log.Logger.Error("RetryVerify 返回Html ：" + resultDetail.Html);
                    httpUtil.RetryVerify();
                    //Log.Logger.Error("RetryVerify 返回Html ：" + resultDetail.Html);

                    resultDetail = httpUtil.HttpGet(url);
                    return resultDetail;

                }
                if (resultDetail.Html.Contains("<script>location.href=\"/user_login\"</script>"))
                {
                    Log.Logger.Error("<script>location.href=\"/user_login\"</script>" + resultDetail.Html);
                }
                return resultDetail;

            }
            catch (Exception ex)
            {
                Log.Logger.Error("HttpUtil，登录失败" + ex.ToString());
                resultDetail.Passed = false;
                resultDetail.Message = ex.ToString();
                return resultDetail;
            }
        }

        public ResultModel HttpPost(List<string> param, string url)
        {
            ResultModel resultSearch = new ResultModel();
            try
            {
                HttpUtil httpUtil = new HttpUtil();
                //通过关键字查询
                resultSearch = httpUtil.HttpPostForm(param, url);
                if (resultSearch.Html.Contains("您的操作过于频繁，验证后再操作"))
                {
                    httpUtil.RetryVerify();
                    resultSearch = httpUtil.HttpPostForm(param, url);
                }
                return resultSearch;

            }
            catch (Exception ex)
            {
                Log.Logger.Error("对外投资翻页" + ex.ToString());
                resultSearch.Passed = false;
                resultSearch.Message = ex.ToString();
                return resultSearch;
            }

        }

        public ResultModel HttpPostJson(string json, string url)
        {
            ResultModel resultSearch = new ResultModel();
            try
            {
                HttpUtil httpUtil = new HttpUtil();
                //通过关键字查询
                resultSearch = httpUtil.HttpPostJson(json, url);
                if (resultSearch.Html.Contains("您的操作过于频繁，验证后再操作"))
                {
                    httpUtil.RetryVerify();
                    resultSearch = httpUtil.HttpPostJson(json, url);
                }
                return resultSearch;

            }
            catch (Exception ex)
            {
                Log.Logger.Error("对外投资翻页" + ex.ToString());
                resultSearch.Passed = false;
                resultSearch.Message = ex.ToString();
                return resultSearch;
            }

        }

        public HtmlDocument HttpGetAndDocOP(string url, string Unique)
        {
            HtmlDocument doc = new HtmlDocument();
            try
            {
                ResultModel resultDetail = new ResultModel();
                resultDetail = HttpGet(url, Unique);
                doc.LoadHtml(resultDetail.Html);
                doc = CommonClass.HelperHtmlNode(doc);
                return doc;

            }
            catch (Exception ex)
            {
                Log.Logger.Error("HttpUtil，登录失败" + ex.ToString());
                throw ex;
            }
        }

    }
}
